package ormap.skywalker.callhelper;

import tools.skywalker.callhelper.PublicCfg;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import db.skywalker.callhelper.DB;
import entity.skywalker.callhelper.DataReportEntity;

public class DataReportOR
{
	private final String STR_TABLE_NAME = "data_report";
	private final String STR_COLUMNS_REPORT_ID = "report_id";
	private final String STR_COLUMNS_COUNT = "count";
	private final String STR_COLUMNS_REPORT_TIME = "report_time";
	private final String STR_COLUMNS_IS_REPORT = "is_report";
	
	private SQLiteDatabase db;
	private DB database;
	
	public DataReportOR(Context context)
	{
		database = new DB(context);
	}
	
	public void open()
	{
		db = database.open();
	}
	
	public void close()
	{
		if (null != database)
		{
			database.close();
		}
	}
	
	public Cursor query()
	{
		return db.rawQuery("select * from " + STR_TABLE_NAME 
				+ " limit " + String.valueOf(PublicCfg.DATA_REPORT_MAX_COUNT), 
				null);
		
	}
	
	public long insert(DataReportEntity dataReportEntity)
	{
		ContentValues cv = new ContentValues();
		cv.put(STR_COLUMNS_REPORT_ID, String.valueOf(dataReportEntity.getReportId()));
		cv.put(STR_COLUMNS_COUNT, String.valueOf(dataReportEntity.getCount()));
		cv.put(STR_COLUMNS_REPORT_TIME, dataReportEntity.getReportTime());
		cv.put(STR_COLUMNS_IS_REPORT, dataReportEntity.getIsReport());
		return db.insert(STR_TABLE_NAME, null, cv);
	}
	
	public int delete(DataReportEntity dataReportEntity)
	{
		return db.delete(STR_TABLE_NAME, 
				    "_id = " + String.valueOf(dataReportEntity.getId()), 
				    null);
	}
}